var Logic = require('BijiLogic');
var Player = require('Player');
var Macro = require('Macro');
var AI = require('AI');

cc.Class({
    extends: cc.Component,

    properties: {
        decksNum: 1,
        hasKing: true,
        playerNum: 5,
        dealNum: 9, //9张配 10张配 玩法配置
        maxRound: 10, //游戏局数
    },

    // LIFE-CYCLE CALLBACKS:

    ctor () {
    },

    onLoad () {
        this.cardsNum = 54;
        if (!this.hasKing)
            this.cardsNum -= 2 * this.decksNum;
        this.cardsTotalNum = this.cardsNum * this.decksNum;
        this.curRound = 0;
        this.gameStatus = 0;
        this.lastGameStatus = 0;
        this.lo = new Logic();
        this.p = new Player();
        this.p.playerNum = this.playerNum;
        this.p.initPlayers();
        this.uiInit();
        cc.log("players:", this.p.players);
 
        for (var i = 0; i < this.playerNum; i++)
        {
            var p = this.p.players[i];
            if (i > 0) {
                var ai = new AI();
                ai.p = p;
                ai.simulator = this;
                p.ai = ai;
                p.nick = "机器人" + i + "号";
            } else {
                p.nick = "小宝";
            }
            p.score = 0;
            var pl = this.plUsers[i];
            pl.empty.active = false;
            pl.nick.string = p.nick;
            pl.score.string = p.score;
            pl.offline.active = false;
            if (i > 0)
                pl.creator.active = false;
            pl.ready.active = false;
            if (pl.kick) pl.kick.active = false;
        }
    },

    start () {
    },

    update (dt) {
        var ps = this.p.players;
        if (!ps) return;
        var pn = this.playerNum;
        var readyCnt = 0;
        for (var i = 0; i < pn; i++)
        {
            var p = ps[i];
            var pl = this.plUsers[i];
            if (p.status == 3)
            {
                pl.ready.active = true;
                readyCnt ++;
            }
            if (p.ai) p.ai.mUpdate(dt);
        }
        if (readyCnt == pn) this.gameStatus = 1;
        if (this.gameStatus != this.lastGameStatus)
        {
            this.lastGameStatus = this.gameStatus
            if (this.gameStatus == 1)
            {
                this.newGame();
                ClientHandler.post(Macro.game.start);
            }
            else if (this.gameStatus == 2)
            {
                this.curRound ++;
                ClientHandler.post(Macro.game.end);
                if (this.curRound == this.maxRound)
                {
                    ClientHandler.post(Macro.game.over);
                }
            }
        }
    },

    //变量重置
    newGame () {
        var ps = this.p.players;
        var pn = this.playerNum;
        var dn = this.dealNum;
        for (var i = 0; i < pn; i++)
        {
            var p = ps[i];
            p.status = 5;
        }
        this.shuffle();
        for (var i = 0; i < pn; i++)
        {
            var deck = this.deal(dn);
            ps[i].cards.sdata = deck;
            this.transform(ps[i].cards);
        }

        this.enterBtnsPl.active = false;
    },

    //洗牌
    shuffle () {
        var cards = [];
        for (var i = 0, len = this.decksNum; i < len; i++)
        {
            cards = cards.concat(Global.cardsData.slice(0));
            if (!this.hasKing) cards.splice(-2, 2);
        }
        cards.sort(function() { return 0.5 - Math.random() });
        cards.sort(function() { return 0.5 - Math.random() });
        cards.sort(function() { return 0.5 - Math.random() });
        cc.log("shuffle", cards);
        for (var i = 0, len = cards.length; i < len; i++)
        {
            // cc.log(Global.transDex(cards[i]));
        }
        this.decks = cards;
    },

    deal (num) {
        if (!this.decks || this.decks.lenth < this.cardsNum)
        {
            cc.log("decks empty, please call shuffle() at first.");
            return;
        }
        if (!num)
        {
            cc.log("parameter num is nil, tell num for dealing.");
            return;
        }
        return this.decks.splice(0, num);
    },

    transform (cards) {
        cards.data = [];
        for (var i = 0, len = cards.sdata.length; i < len; i++)
        {
            cards.data.push(Global.transDex(cards.sdata[i]));
        }
    },

    uiInit () {
        this.enterBtnsPl = this.node.getChildByName("Panel_5");
        this.enterBtnsPl.active = true;

        this.btnReady = this.node.getChildByName("Panel_6").getChildByName("Button_9");
        this.btnOverShow = this.node.getChildByName("Panel_6").getChildByName("Button_9_0");
        this.btnReady.active = false;
        this.btnOverShow.active = false;

        this.plUsers = [];
        var res = ['Panel_7', 'Panel_7_0', 'Panel_7_0_0', 'Panel_7_0_1', 'Panel_7_0_2'];
        for (var i = 0, len = res.length; i < len; i++)
        {
            var pl = this.node.getChildByName("Panel_6").getChildByName(res[i]);
            pl.empty = pl.getChildByName("Image_52");
            pl.nick = pl.getChildByName("Panel_8").getChildByName("Text_2").getComponent(cc.Label);
            pl.score = pl.getChildByName("Panel_8").getChildByName("Text_2_0").getComponent(cc.Label);
            pl.offline = pl.getChildByName("Panel_8").getChildByName("Image_6");
            pl.kick = pl.getChildByName("Panel_8").getChildByName("Button_1");
            pl.creator = pl.getChildByName("Panel_8").getChildByName("Image_12");
            pl.ready = pl.getChildByName("Panel_8").getChildByName("Image_13_0");
            this.plUsers.push(pl);
        }

        this.plEndings = [];
        var res = ['Panel_11', 'Panel_11_1', 'Panel_11_2', 'Panel_11_3', 'Panel_11_4'];
        for (var i = 0, len = res.length; i < len; i++)
        {
            var pl = this.node.getChildByName("Panel_10").getChildByName(res[i]);
            this.plEndings.push(pl);
        }

        this.plCardsBack = [];
        var res = ['Panel_12', 'Panel_12_1', 'Panel_12_2', 'Panel_12_3', 'Panel_12_4'];
        for (var i = 0, len = res.length; i < len; i++)
        {
            var pl = this.node.getChildByName("Panel_10").getChildByName(res[i]);
            this.plCardsBack.push(pl);
        }
    },
});
